package com.bulemobi.activity.Ac_CarService;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import com.bulemobi.net.tsz.afinal.FinalHttp;
import com.bulemobi.net.tsz.afinal.annotation.view.ViewInject;
import com.bulemobi.net.tsz.afinal.http.AjaxCallBack;
import com.bulemobi.net.tsz.afinal.http.AjaxParams;
import com.bulemobi.urlCentre.UrlCentre;
import com.bulemobi.util.MD5Util;
import com.bulemobi.view.BaseActivity;
import com.bulemobi.view.titleView.ActionBarView;
import com.jsyh.hcb.R;

import java.security.NoSuchAlgorithmException;

/**
 * 应急服务
 * Created by 武 on 2014/9/19 0019.
 */
public class Ac_Emergency extends BaseActivity implements View.OnClickListener {
    //    @ViewInject(id = R.id.lv_emergency)
//    PullToRefreshListView lv_emergency;
    @ViewInject(id = R.id.actionBar)
    ActionBarView actionBarView;
    @ViewInject(id = R.id.lay_add)
    LinearLayout lay_add;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_emergency_main);
        initView();
        testRequest();
        testRequest1();
    }

    private void testRequest1() {
        AjaxParams params = new AjaxParams();
        String value = UrlCentre.APPSECRET + "city" + "南京" + "appkey" + UrlCentre.APPKEY + UrlCentre.APPSECRET;
        String sign = "";
        try {
            sign = MD5Util.encodeByMD5(value);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        params.put("appkey", UrlCentre.APPKEY);
        params.put("city", "南京");
        params.put("sign", sign);
        params.put("sign_method", "MD5");
        params.put("timestamp", getNowDate());
        requestHttp(UrlCentre.URL_BREAK, params);
    }

    private void testRequest() {
        AjaxParams params = new AjaxParams();
        String value = UrlCentre.APPSECRET + "city" + "南京" + "appkey" + UrlCentre.APPKEY + UrlCentre.APPSECRET;
        String sign = "";
        try {
            sign = MD5Util.encodeByMD5(value);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        params.put("appkey", UrlCentre.APPKEY);
        params.put("city", "南京");
        params.put("sign", sign);
        params.put("sign_method", "MD5");
        params.put("timestamp", getNowDate());
        requestHttp(UrlCentre.URL_SORT, params);
    }

    private void initView() {
        actionBarView.button_right_text.setVisibility(View.VISIBLE);
        actionBarView.button_right_text.setText("代驾");
        actionBarView.button_right_text.setTextColor(Color.WHITE);
        actionBarView.button_right_text.setOnClickListener(this);
        actionBarView.title.setText(getResources().getString(R.string.ac_emergency));
        lay_add.setOnClickListener(this);
    }

    private void requestHttp(String url, AjaxParams params) {
        FinalHttp http = new FinalHttp();
        http.get(url, params, new AjaxCallBack<Object>() {
            @Override
            public void onStart() {
                showProgressDialog();
                super.onStart();
            }

            @Override
            public void onSuccess(Object o) {
                Log.d("OnSuccess", o.toString());
                closeProgressDialog();
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                closeProgressDialog();
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.actionbar_right_button_text:
                openActivity(Ac_ReplaceDriver.class);
                break;
            case R.id.lay_add:
                openActivity(Ac_add_UrgencyThing.class);
                break;
        }
    }
}
